﻿@{
    ViewBag.Title = "选择用户";
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}

<div class="dialog-content">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-horizontal">
        <ul class="wysihtml5-toolbar">
            <li class="toolbar-right">
                <div class="input-append input-icon-prepend">
                    <div class="add-on">
                        <a title="search" class="icon"><i class="icofont-search"></i></a>
                        <input type="text" id="searchkey" data-fieldname="searchkey" placeholder="输入用户名或姓名查询">
                    </div>
                    <button id="btnquery" class="btn btn-small">查询</button>
                </div>
            </li>
        </ul>
    </div>
    <div data-autosize="true" style="height:440px;">
        <table id="datagrid" data-options="fit:true"></table>
    </div>
</div>
<div class="dialog-footer">
    <button id="btnCreateuser" class="btn btn-small btn-success" style="margin-left:30px;float:left;display:none;">创建新用户</button>
    <button id="btnclose" class="btn btn-small btn-default">关闭</button>
    <button id="btnok" class="btn btn-small btn-primary">确定</button>

</div>
@section scripts{
    <script type="text/javascript">
        //回调函数
        var callback = "@ViewBag.CallBack";
        //是否单选
        var singleselect = @(ViewBag.SingleSelect ? "true" : "false");
        var ccode = "@ViewBag.custcode";
        var siteid="@ViewBag.Siteid";
        var studyid="@ViewBag.Studyid";

        //列定义
        var cols = [[
                { field: "Id", title: 'Id', width: 10, hidden: true },
                { field: "ck", title: '', checkbox:true, width: 30, align: 'center', hidden:singleselect },
                { field: "Username", title: '登录名', width: 100, sortable: true, align: 'left' },
                { field: "Name", title: '姓名', width: 100, sortable: true, align: 'left' },
                { field: "Sex", title: '性别', width: 40, sortable: true, align: 'center' },
                { field: "Createtime", title: '创建时间', width: 100, sortable: true, align: 'left' },
                { field: "Remark",title:'备注',width:100,sortable:true,align:'left'}
        ]];

        $(document).ready(function () {
            //关闭
            $("#btnclose").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });
            //创建新用户

            if ("@ViewBag.Frompage"=="list") {
                $("#btnCreateuser").css("display","inline");
            }

            $("#btnCreateuser").click(function(evt){
                evt.preventDefault();
                parent.setDialogTitle("创建新用户");
                document.location.href= baseurl + "Manage/User/Edit/0?f=study&studyid="+studyid+"&sid="+siteid;
            });

            //添加
            $("#btnok").click(function (evt) {
                evt.preventDefault();
                //  debugger;
                var jsondata = [];

                var selectedRows = $("#datagrid").datagrid("getSelections");

                if(selectedRows.length==0){
                    //alert("请选择一行数据！");
                    $.messager.alert("提醒", csms_lang.message_choose_an_existing, "warning");
                    return;
                }
                if (singleselect) {
                    //验证用户是否在该中心已存在
                    $.ajax({
                        type: "post",
                        url: baseurl + "Public/UserExsit",
                        data: {id:selectedRows[0].Id,siteid:siteid,studyid: studyid },
                        success: function (data) {
                            if (data=="no") {
                                $.messager.alert("提醒", "选择的用户已经是该课题成员！", "waring");
                                return ;
                            }
                            else
                            {
                                parent.setDialogTitle("新增研究人员");
                                document.location.href= baseurl + "Study/Site/Siteuseredit/0?studyid=" + studyid + "&sid="+siteid+"&uid="+selectedRows[0].Id+"&" + Math.random();
                            }
                        }
                    });

                }
                else
                {
                    parent.eval(callback + "('" + JSON.stringify(selectedRows) + "')");
                    parent.closeDialog();
                }

            });
            //查询
            $("#btnquery").click(function (evt) {
                evt.preventDefault();
                var params = $("#datagrid").datagrid('options').queryParams;
                params.custcode=ccode;
                params.s = $("#searchkey").val();
                $("#datagrid").datagrid("load");
                $("#datagrid").datagrid("clearSelections");
            });

            $("#searchkey").keydown(function (event) {
                if (event.keyCode == $.ui.keyCode.ENTER) {
                    event.preventDefault();
                    $("#btnquery").click();
                    return false;
                }
            });

            LoadDatagrid();
        });

        //加载列表数据
        function LoadDatagrid() {
            var $dg = $("#datagrid");
            $dg.datagrid({
                //title: '子项目字典',
                url: baseurl + 'Public/UserListJson',
                fit: true,
                fitColumns: true,
                striped: true,
                nowrap: true,
                idField: 'Id',
                pagination: true,
                pageNumber: 1,
                pageSize: 100,
                pageList: [10,15, 20, 30, 50, 80, 100],
                rownumbers: true,
                singleSelect: singleselect,
                queryParams: {
                    custcode:ccode,
                    studyid:studyid,
                    s: ""
                },
                sortName: 'Id',
                sortOrder: 'asc',
                columns: cols,
                onLoadSuccess: function () {
                    //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
                    //$dg.datagrid("clearSelections");
                },
                onDblClickRow: function (rowIndex, row) {
                    $("#btnok").click();
                },
                //onSelect: function (rowIndex, row) {
                //    var selectedRows = $dg.datagrid("getSelections");
                //    if (selectedRows.length == 0) {
                //        $("#btnok").attr("disabled", true);
                //    } else {
                //        $("#btnok").attr("disabled", false);
                //    }
                //},
                //onUnselect: function (rowIndex, row) {
                //    var selectedRows = $dg.datagrid("getSelections");
                //    if (selectedRows.length == 0) {
                //        $("#btnok").attr("disabled", true);
                //    } else {
                //        $("#btnok").attr("disabled", false);
                //    }
                //}
            });
            //PaginationInit();
        }
    </script>
}